import {messages} from "share/common";
import React from 'react'
import { connect } from 'react-redux'


import { Table, Popconfirm, Button } from 'antd'
import config from 'config'

import budgetBalanceQueryService from 'containers/budget/budget-balance-query/budget-balance-query.service'

class BudgetBalanceCondition extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      loading: true,
      data: [],
      columns: [
        {title: messages('budget.key35'/*方案代码*/), dataIndex: 'conditionCode', width: '35%'},
        {title: messages('budget.key36'/*方案名称*/), dataIndex: 'conditionName', width: '35%'},
        {title: messages('budget.key37'/*操作*/), dataIndex: 'operation', width: '30%', render: (text, record) => (
          <span>
            <a onClick={(e) => this.useCondition(e, record)}>{messages('budget.key38'/*应用现有方案*/)}</a>
            <span className="ant-divider" />
            <Popconfirm onConfirm={(e) => this.deleteCondition(e, record)} title={messages('budget.key39'/*你确定要删除这条数据吗？*/)}>
              <a onClick={(e) => {e.preventDefault();e.stopPropagation();}}>{messages('common.delete')}</a>
            </Popconfirm>
          </span>)}
      ]
    };
  }

  componentDidMount(){
    this.getList();
  };

  getList = () => {
    this.setState({ loading: true });
    return budgetBalanceQueryService.getSolution().then(res => {
      this.setState({ data: res.data, loading: false })
    })
  };

  useCondition = (e, record) => {
    this.setState({ loading: true });
    budgetBalanceQueryService.getBudgetBalance(record.id).then(res => {
      this.setState({ loading: false });
      this.props.close(res.data);
    })
  };

  deleteCondition = (e, record) => {
    this.setState({ loading: true });
    budgetBalanceQueryService.deleteBudgetBalance(record.id).then(res => {
      this.setState({ loading: false });
      this.getList();
    })
  };

  onCancel = () => {
    this.props.close();
  };

  render(){
    const { columns, data, loading } = this.state;
    return (
      <div>
        <Table columns={columns}
               loading={loading}
               dataSource={data}
               size="middle"
               bordered rowKey="id"/>
        <div className="slide-footer">
          <Button type="primary" onClick={this.getList} loading={loading}>{messages('budget.key40'/*刷新列表*/)}</Button>
        </div>
      </div>
    )
  }

}

function mapStateToProps() {
  return {}
}

export default connect(mapStateToProps)(BudgetBalanceCondition);
